package com.syni.mdd.sjb.common.datatreaure.listen.client;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankClientGroup;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankGroup;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.datatreaure.service.DankClientGroupService;
import com.syni.mdd.sjb.common.datatreaure.service.DankGroupService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Log4j2
public class DataClientListener implements ApplicationListener<ClientNavigateEvent> {

    @Autowired
    DankClientGroupService dankClientGroupService;
    @Autowired
    DankGroupService dankGroupService;

    @Override
    @Async
    public void onApplicationEvent(ClientNavigateEvent clientNavigateEvent) {
        List<DankGroup> dankGroups = dankGroupService.getBaseMapper().selectList(new QueryWrapper<>());
        Integer userId = clientNavigateEvent.getUserId();
        List<DankClientGroup> dankClientGroupList = dankClientGroupService.findByUserId(userId);
        if(!CollectionUtils.isNotEmpty(dankClientGroupList)){
            List<DankClientGroup> newDankClientGroupList = BeanUtils.copyListIgnore(dankGroups, DankClientGroup.class,"id","newTime");
            for(DankClientGroup d : newDankClientGroupList){
                d.setUserId(clientNavigateEvent.getUserId());
            }
            dankClientGroupService.saveBatch(newDankClientGroupList);
        }
    }
}
